<?php

/**
 * Created by Shijie Liu.
 * 通用的curl类，获取html
 */
set_time_limit(0);
class LectureSpider
{

    /*
     * 一次爬取过程，获取一个网站de讲座信息
     * @param $url
     */
    function get_lecture_info($url, $pattern)
    {
        $lecture_info = $this->get_info($url, $pattern);
        return $lecture_info;
    }

    /*
     * curl抓取html文档
     * @param $url
     * @return HTML文档
     */
    function get_html($url)
    {
        $con = curl_init();
        curl_setopt($con, CURLOPT_URL, $url);
        curl_setopt($con, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($con,CURLOPT_USERAGENT,"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36");

        $html = curl_exec($con);
        $http_state = curl_getinfo($con);
        curl_close($con);

        if ($http_state["http_code"] == 200) {
	        $html = mb_convert_encoding($html, 'utf-8', 'gb2312,utf-8,gbk');  //convert the encoding
            return $html;
        } else {
            return "Connection Failed" . curl_error($con);
        }

    }

    /*
     * 获取讲座信息，标题、日期、链接
     * @param $url
     * @return 返回info数组
     */
    function get_info($url, $pattern)
    {

        $content = $this->get_html($url);
        preg_match_all($pattern, $content, $arr_all);     //$arr_all,全文检索中关键词

//        for ($i = 0;$i < count($arr_all[0]);$i++){
//            echo trim($arr_all[2][$i] . $arr_all[3][$i] . $arr_all[4][$i]);
//            echo "\n";
//        }

        $info = array();      //$info，1个网站的讲座信息，二维数组，键为title、URL、date

        if (!empty($arr_all)) {

            //获取前缀url，拼接讲座页面url
            $pattern_part = "/^(\w+:\/\/)?([^\/]+)/i";
            preg_match($pattern_part, $url, $arr_url);
            if (!empty($arr_url)) {
                $pre_url = trim($arr_url[0]);  //前缀URL
            }

            //判断日期，符合条件加入$info
            for ($i = 0; $i < count($arr_all[0]); $i++) {
                $lec_url = $pre_url . "/" . trim($arr_all[1][$i]);
                $date = $this->get_date($lec_url);

                if (is_null($date)) {
                    continue;
                } else {
                    $info[$i]["title"] = trim($arr_all[2][$i] . $arr_all[3][$i] . $arr_all[4][$i]);
                    $info[$i]["date"] = $date;
                    $info[$i]["url"] = $lec_url;
                }
            }
        }
        return $info;
    }

    /*
     * 取日期，判断试是否在7天之内，返回日期值
     * @param $url
     * @return $date
     */
    function get_date($url)
    {

        $pattern = '/\d{4}(-|\.)\d{2}(-|\.)\d{2}/';
        $lec_content = $this->get_html($url);
        preg_match($pattern, $lec_content, $dates);

        if (!empty($dates)) {
            $date_lec = preg_replace("/-|\./", "-", $dates[0]);  //统一转换成xxxx-xx-xx格式
            $date_now = date("Y-m-d");
            $days_space = (strtotime($date_now) - strtotime($date_lec)) / 3600 / 24;  //相差的天数

            if ($days_space <= 10) {
                $date = $date_lec;
            } else {
                $date = null;
            }
        } else {
            $date = null;
        }
        return $date;
    }
}